{include file="header.tpl"}
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
var prevPartyFine = 0;
var prevPartyAmount = 0;
	
$(document).ready(function()
{
  $(".delete").live('click', function(event) 
  {
    $(this).parent().remove();
    setNetWeight(this);
    setTotNetWeight(this);
    totFine(this);
    setAmount(this);
    setTotAmount(this);
  });
});

function addNewRow()
{
  $('#billBook tr:last').after('<tr class="itemRow"><td><input type="text" name="particulars[]" \/><\/td><td><input type="text" name="weight[]" class="weight" onChange="setNetWeight(this)"\/><\/td><td><input type="text" name="pari[]" class="pari" onChange="setNetWeight(this)"\/><\/td><td><input type="text" name="netWeight[]" class="netWeight" \/><\/td><td><input type="text" name="touch[]" class="touch" onChange="setFine(this)"\/><\/td><td><input type="text" name="waste[]" class="waste" onChange="setFine(this)"\/><\/td><td><input type="text" name="fine[]" class="fine" \/><\/td><td><input type="text" name="particular[]" class="particular" \/><\/td><td><input type="text" name="qty[]" class="qty" onChange="setAmount(this)"\/><\/td><td><input type="text" name="labour[]" class="labour" onChange="setAmount(this)"\/><\/td><td><input type="text" name="amount[]" class="amount" \/><\/td><td><a href="javascript:addNewRow();">{$l_add}<\/a><\/td><td class="delete">{$l_remove}<\/td><\/tr>');

}

function setNetWeight(txtObj)
{
  var row = $(txtObj).parents('.itemRow');
  var price = parseFloat(row.find('.weight').val().replace("$","")) - parseFloat(row.find('.pari').val());
  isNaN(price) ? row.find('.netWeight').val("") : row.find('.netWeight').val(price);
  setTotNetWeight();
  setFine(txtObj);
}

function setTotNetWeight()
{
  var totalWeight = 0;
  var totalpari   = 0;
  var totalNetWeight = 0;
  
  $('.weight').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalWeight += Number(price);
  });
  
  $('.pari').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalpari += Number(price);
  });
  
  $('.netWeight').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalNetWeight += Number(price);
  });

  $('#totWeight').val(totalWeight);
  $('#totPari').val(totalpari);
  $('#totNetWeight').val(totalNetWeight);
}

function setFine(txtObj)
{
  var row = $(txtObj).parents('.itemRow');
  var price = (parseFloat(row.find('.netWeight').val()) * (parseFloat(row.find('.touch').val().replace("$","")) + parseFloat(row.find('.waste').val())) / 100);
  isNaN(price) ? row.find('.fine').val("") : row.find('.fine').val(price.toFixed(0));
  totFine();
}

function totFine()
{
  var totFine = 0;
  
  $('.fine').each(function(i)
  {
    price = $(this).val().replace("$","");
    totFine += Number(price);
    $('#totFine').val(totFine.toFixed(0));
  });
  
  $('#totFine').val();
    $('#partyFine').val(parseFloat(prevPartyFine)) + parseFloat(totFine);
}

function setAmount(txtObj)
{
  var row = $(txtObj).parents('.itemRow');
  var price = parseFloat(row.find('.qty').val().replace("$","")) * parseFloat(row.find('.labour').val());
  isNaN(price) ? row.find('.amount').val("") : row.find('.amount').val(price);
  setTotAmount();
}

function setTotAmount()
{
  var totQty    = 0;
  var totLabour = 0;
  var totAmount = 0;
  
  $('.amount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totAmount += Number(price);
  });
  $('#totAmount').val(totAmount);
  $('#partyAmount').val(parseFloat(prevPartyAmount) + parseFloat(totAmount));
  
  $('.labour').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totLabour += Number(price);
  });
  $('#totLabour').val(totLabour);
  
  $('.qty').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totQty += Number(price);
  });
  $('#totQty').val(totQty);
}

  function getOldAmount()
  {
	if(document.form1.party.value > 0)
	{
      $.ajax({
	    type:"POST",
	    url:'getOldAmountVepaari.php',
	    data:
	    {
	      partyId : document.form1.party.value,
	      billNo  : document.form1.billBookNo.value,
	    },
	    success: function(response)
	    {
	    	var temp = response.split(",");
	    	prevPartyFine = temp[0];
	    	prevPartyAmount = temp[1];
	    	$('#partyFine').val(prevPartyFine);
	    	$('#partyAmount').val(prevPartyAmount);
	    	$('#fixedPartyFine').val(prevPartyFine);
	    	$('#fixedPartyAmount').val(prevPartyAmount);
	    }
    });
	}
}

function hidePrevDiv(chkObj)
{
	if(chkObj.checked)
	  $('#oldTotal').show();
	else
	  $('#oldTotal').hide();
}
</script>
<form name="form1" action="" method="post">
<input type="hidden" name="isEdit" value="{$isEdit}" />
<input type="hidden" name="billNo" value="{$billBookNo}"   />
<input type="hidden" name="billBookId" value="{$billBookId}" />
<input type="hidden" name="fixedPartyFine"   id="fixedPartyFine"   value="" />
<input type="hidden" name="fixedPartyAmount" id="fixedPartyAmount" value="" />


<center><h4>{$l_billBook}</h4></center>
{if $msg != ''}<center><span style="color:#FF0000"><b>{$msg}</b></span></center>{/if}
<div class="contentDiv">
  <table border="0" id="billBook" align="center" width="100%">
  <tr>
    <td colspan="5" class="table1">
      {$l_billBookDetailId} 
      <input type="text" name="billBookNo" size="5" value="{$billBookNo}" readonly />&nbsp;
      {$l_partySelect} :
      {if $isEdit eq 1}
      <select name="party" onChange="getOldAmount();" autofocus Disabled />
        {html_options values=$party.partyId output=$party.partyName selected=$partyId}
      </select>
      {else}
      <select name="party" onChange="getOldAmount();" autofocus  />
        {html_options values=$party.partyId output=$party.partyName selected=$partyId}
      </select>
      {/if}
    </td>
    <td colspan="6" class="table1">
      {$l_billBookDate} : 
      {html_select_date prefix="curDate" start_year="-2" end_year="+1" field_order="DMY" day_value_format="%02d" time=$billDate}
    </td>
    <td class="table1"><input type="checkbox" name="removePrev" onChange="hidePrevDiv(this);" CHECKED /></td>
  </tr>
  <tr class="table1">
    <th>{$l_particulars}</th>
    <th>{$l_weight}</th>
    <th>{$l_pari}</th>
    <th>{$l_netWeight}</th>
    <th>{$l_touch}</th>
    <th>{$l_waste}</th>
    <th>{$l_fine}</th>
    <th>Particular</th>
    <th>{$l_qty}</th>
    <th>{$l_labour}</th>
    <th>{$l_amount}</th>
    <th colspan="2">&nbsp;</th>
  </tr>
  {if $isEdit == 1}
    {section name="sec" loop=$billBookArr|@count}
      <tr class="itemRow">
        <td><input type="text" name="particulars[]" value="{$billBookArr[sec]['particulars']}" /></td>
        <td><input type="text" name="weight[]" class="weight" onChange="setNetWeight(this)" value="{$billBookArr[sec]['weight']}" /></td>
        <td><input type="text" name="pari[]" class="pari" onChange="setNetWeight(this)" value="{$billBookArr[sec]['pari']}" /></td>
        <td><input type="text" name="netWeight[]" class="netWeight" value="{$billBookArr[sec]['netWeight']}" /></td>
        <td><input type="text" name="touch[]" class="touch" onChange="setFine(this)" value="{$billBookArr[sec]['touch']|string_format:"%.0f"}" /></td>
        <td><input type="text" name="waste[]" class="waste" onChange="setFine(this)" value="{$billBookArr[sec]['waste']|string_format:"%.0f"}" /></td>
        <td><input type="text" name="fine[]" class="fine" value="{$billBookArr[sec]['fine']|string_format:"%.2f"}" /></td>
        <td><input type="text" name="particular[]" class="particular" value="{$billBookArr[sec]['particular']}" /></td>
        <td><input type="text" name="qty[]" class="qty" onChange="setAmount(this)" value="{$billBookArr[sec]['qty']}" /></td>
        <td><input type="text" name="labour[]" class="labour" onChange="setAmount(this)" value="{$billBookArr[sec]['labour']}" /></td>
        <td><input type="text" name="amount[]" class="amount" value="{$billBookArr[sec]['amount']}" /></td>
        <td><a href="javascript:addNewRow();">{$l_add}</a></td>
         <td class="delete">{$l_remove}</td>
      </tr>
    {/section}
  {else}
  <tr class="itemRow">
    <td><input type="text" name="particulars[]" /></td>
    <td><input type="text" name="weight[]" class="weight" onChange="setNetWeight(this)"/></td>
    <td><input type="text" name="pari[]" class="pari" onChange="setNetWeight(this)"/></td>
    <td><input type="text" name="netWeight[]" class="netWeight" /></td>
    <td><input type="text" name="touch[]" class="touch" onChange="setFine(this)"/></td>
    <td><input type="text" name="waste[]" class="waste" onChange="setFine(this)"/></td>
    <td><input type="text" name="fine[]" class="fine" /></td>
    <td><input type="text" name="particular[]" class="particular" /></td>
    <td><input type="text" name="qty[]" class="qty" onChange="setAmount(this)"/></td>
    <td><input type="text" name="labour[]" class="labour" onChange="setAmount(this)"/></td>
    <td><input type="text" name="amount[]" class="amount" /></td>
    <td><a href="javascript:addNewRow();">{$l_add}</a></td>
    <td class="delete">{$l_remove}</td>
    <td>&nbsp;</td>
  </tr>
  {/if}
  </table>
  
  <table width="100%">
    <tr class="itemRowTot">
      <th>{$l_total}</th>
      <th><input type="text" name="totWeight" id="totWeight"/></th>
      <th><input type="text" name="totPari" id="totPari"/></th>
      <th><input type="text" name="totNetWeight" id="totNetWeight"/></th>
      <th align="right" width="105px;">&nbsp;</th>
      <th align="right" width="105px;">&nbsp;</th>
      <th><input type="text" name="totFine" id="totFine"/></th>
      <th><input type="text" name="totQty" id="totQty"/></th>
      <th><input type="text" name="totLabour" id="totLabour"/></th>
      <th><input type="text" name="totAmount" id="totAmount"/></th>
      <th colspan="2" width="70px;"></th>
    </tr>
  </table>
  <div id="oldTotal" style="float:right;">
  	<b>Prev. Fine</b> <input type="text" name="partyFine" id="partyFine" />
  	<b>Prev. Amount</b> <input type='text' name='partyAmount' id='partyAmount' />
  </div>
  {if $isEdit == 1}
    <input type="submit" name="billBookAddBtn" value="{$l_save}"/>
    <input type="submit" name="cancelBtn" value="Cancel"/>
  {else}
    <input type="submit" name="billBookAddBtn" value="{$l_addBillBook}"/>
  {/if}
</div>
</form>
<script type="text/javascript">
{if $isEdit == 1}
  setTotNetWeight();
  totFine();
  setTotAmount();
{/if}  
</script>
{include file="footer.tpl"}